Unity in Action, Third Edition by Joseph Hocking
Author:Joseph Hocking [Hocking, Joseph]
Language: eng
Format: epub, mobi, pdf
Publisher: Manning Publications Co.
Published: 0101-01-01T00:00:00+00:00
Figure 8.10 Diagram of raycasting downward while stepping off a ledge
In this case, the code should make the character slide off the ledge. The character will still fall (because itâs not standing on the ground), but itâll also push away from the point of collision (because it needs to move the capsule away from the platform itâs hitting). Thus, the code will detect collisions with the character controller and respond to those collisions by nudging away. This listing adjusts the vertical movement with everything we just discussed.
Listing 8.5 Using raycasting to detect the ground
... private ControllerColliderHit contact; ⶠ... bool hitGround = false; RaycastHit hit; if (vertSpeed < 0 && ⷠPhysics.Raycast(transform.position, Vector3.down, out hit)) { float check = ⸠(charController.height + charController.radius) / 1.9f; hitGround = hit.distance <= check; } if (hitGround) { ⹠if (Input.GetButtonDown("Jump")) { vertSpeed = jumpSpeed; } else { vertSpeed = minFall; } } else { vertSpeed += gravity * 5 * Time.deltaTime; if (vertSpeed < terminalVelocity) { vertSpeed = terminalVelocity; } if (charController.isGrounded) { ⺠if (Vector3.Dot(movement, contact.normal) < 0) { ⻠movement = contact.normal * moveSpeed; } else { movement += contact.normal * moveSpeed; } } } movement.y = vertSpeed; movement *= Time.deltaTime; charController.Move(movement); } void OnControllerColliderHit(ControllerColliderHit hit) { ⼠contact = hit; } }
â¶ Needed to store collision data between functions
â· Check if the player is falling.
⸠Distance to check against (extend slightly beyond the bottom of the capsule)
â¹ Instead of using isGrounded, check the raycasting result.
⺠Raycasting didnât detect ground, but the capsule is touching the ground.
â» Respond slightly differently depending on whether the character is facing the contact point.
â¼ Store the collision data in the callback when a collision is detected.
This listing contains much of the same code as the previous listing; the new code is interspersed throughout the existing movement script, and this listing needs the existing code for context. The first line adds a new variable to the top of the RelativeMovement script. This variable is used to store data about collisions between functions.
The next several lines do raycasting. This code also goes below horizontal movement but before the if statement for vertical movement. The actual Physics.Raycast() call should be familiar from previous chapters, but the specific parameters are different this time. Although the position to cast a ray from is the same (the characterâs position), the direction will be down this time instead of forward. Then, you check how far away the raycast was when it hit something; if the distance of the hit is at the distance of the characterâs feet, the character is standing on the ground, so set hitGround to true.
WARNING The way the check distance is calculated is not obvious, so letâs go over that in detail. First, take the height of the character controller (which is the height without the rounded ends) and then add the rounded ends. Divide this value in half because the ray was cast from the middle of the character (that is, already halfway down) to get the distance to the bottom of the character.
Download
Unity in Action, Third Edition by Joseph Hocking.mobi
Unity in Action, Third Edition by Joseph Hocking.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Blood, Sweat, and Pixels by Jason Schreier(3472)
Dawn of the New Everything by Jaron Lanier(2675)
Godot 4 Game Development Cookbook by Jeff Johnson(2446)
The Art of Doom by Bethesda(2033)
Significant Zero by Walt Williams(1875)
Creative Character Design by Bryan Tillman(1828)
World of Warcraft Chronicle Volume 3 by Blizzard Entertainment(1650)
The Ultimate Roblox Book by David Jagneaux(1610)
Art Of Atari by Tim Lapetino(1557)
Pillars of Eternity Guidebook by Obsidian Entertainment(1530)
Dawn of the New Everything: Encounters with Reality and Virtual Reality by Jaron Lanier(1527)
1628927445Game by Unknown(1467)
Unreal Engine 4 Virtual Reality Projects by Kevin Mack(1459)
Unreal Engine Virtual Reality Quick Start Guide by Jessica Plowman(1436)
Mission Python by Sean McManus(1421)
The Ultimate Player's Guide to Minecraft by Stephen O'Brien(1412)
Learning D by 2015(1408)
Unity 2018 By Example by Alan Thorn(1387)
Road Games by Road Games(1360)
